<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
<!--      <el-form-item label="院校名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入院校名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>-->

      <el-form-item>
<!--        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>-->
        <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['by:classify:add']" >添加</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="List" row-key="id" border default-expand-all>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="上级名称" align="center" prop="pidname" />
      <el-table-column label="创建时间" align="center" prop="addtime" width="180" />
      <el-table-column label="操作" width="280" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Plus" @click="addHandleUpdate(scope.row)" v-hasPermi="['by:post:edit']">添加下级</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['by:post:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['by:post:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />


    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="上级名称" prop="postName">
          <el-input v-model="form.pidname" disabled placeholder="上级名称" />
        </el-form-item>
        <el-form-item label="名称" prop="postName">
          <el-input v-model="form.name" placeholder="请输入学科名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import { addClassify,listClassify, delClassify, getClassify, updateClassify } from "@/api/bydll/classify";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");
const showSearch = ref(true);
const List = ref([]);
const open = ref(false);
const title = ref("");
const loading = ref(true);
const total = ref(0);
const ids = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined
  },
  rules: {
    name: [{ required: true, message: "院校名称不能为空", trigger: "blur" }]
  }
});
const { queryParams, form, rules } = toRefs(data);

function handleQuery(){
  queryParams.value.pageNum = 1;
  getList();
}

function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除？').then(function() {
    return delClassify(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}


function getList() {
  loading.value = true;
  listClassify(queryParams.value).then(response => {
    console.log(response.data);
    List.value = response.data;
    // total.value = response.total;
    loading.value = false;
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateClassify(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClassify(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getClassify(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学科分类";
  });
}
function addHandleUpdate(row){
  reset();
  form.value.pid = row.id;
  form.value.pidname = row.name;
  title.value = "添加学科分类";
  open.value = true;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  form.value.pid = 0;
  form.value.pidname = "顶级选项";
  title.value = "添加学科分类";
  open.value = true;
}
/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    pid: undefined,
    pidname: undefined
  };
  proxy.resetForm("postRef");
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

getList();
</script>

<style scoped>

</style>